package com.springboot.demo.controller;

import com.springboot.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller // 不能使用RestController注解
@RequestMapping("/test")
public class TestController {

    /**
     * 访问/test/hello 跳转到hello页面
     *
     * @param model
     * @param id    前台传入参数
     * @return
     */
    @RequestMapping("/hello")
    public String hello(Model model, String id) {
        // 返回前台数据
        model.addAttribute("hello", "hello welcome!");

        // 获取前台表单传参
        System.out.println("id:" + id);

        // 集合数据用于静态页面foreach
        List<User> users = new ArrayList<>();
        users.add(new User(1, "张三", "深圳"));
        users.add(new User(2, "丽丝", "上海"));
        users.add(new User(3, "王武", "北京"));
        model.addAttribute("users", users);

        // Map 定义
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("No", "123");
        dataMap.put("address", "深圳");
        // 添加数据到model中
        model.addAttribute("dataMap", dataMap);

        // 数组
        String[] names = {"张三", "李四", "王五", "赵六"};
        model.addAttribute("names", names);

        // 日期
        model.addAttribute("now", new Date());

        // if条件参数
        model.addAttribute("age",23);

        return "demo";  // 返回的thymeleaf页面
    }
}
